<template>
  <div class="login">
    <div class="left">
      <div class="main">
        <img class="t1" src="@/assets/img/login_logo.png" alt />
        <span class="t2">李易峰会所</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <!-- 表单
      el-form  :model数组绑定  label-width:标题宽度
          el-form-item  label:标题
        el-input
            v-model:双向绑定
            prefix-icon前面图标
            show-password
    栅栏布局
       el-row  行  24列
          el-col  :span="多少列"
    el-checkbox  v-model:双向绑定  label="选中该项后的值"
         v-model的值为字符串或者boolean值时,它选中某项后它的值为true,取消就为false
         v-model的值为数组 它选中某项后它的值为该数组添加该项,取消就为该数组删除该项 
    el-button  type="primary" 
      -->
      <el-form :model="form" class="form" :rules="rules" ref="form">
        <!-- 输入手机号 -->
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            prefix-icon=" el-icon-user"
          ></el-input>
        </el-form-item>
        <!-- 输入密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon=" el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 输入验证码 -->
        <el-form-item prop="code">
          <el-row>
            <el-col :span="18">
              <el-input
                v-model="form.code"
                placeholder="请输入验证码"
                prefix-icon=" el-icon-key"
              ></el-input>
            </el-col>
            <el-col :span="6">
              <img class="login_img" @click="gocode" :src="codeUrl" alt />
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 我已阅读 -->
        <el-form-item prop="ispass">
          <el-checkbox v-model="form.ispass" class="ispass" :label="true">
            我已阅读并同意
            <el-link type="primary" class="tiao">用户协议</el-link>和
            <el-link type="primary" class="tiao">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <!-- 登录注册按钮 -->
        <el-form-item>
          <el-button type="primary" class="btn" @click="tologin"
            >登录</el-button
          >
          <br />

          <el-button type="primary" class="btn" @click="cancleEven"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <img class="right" src="@/assets/img/login.png" alt />
    <register ref="register"></register>
  </div>
</template>

<script>
// 导入子组件
import register from "./register";

import { userLogin } from "@/api/login.js";

import { saveLocal, getLocal } from "@/utils/local.js";
export default {
  components: {
    register,
  },
  data() {
    return {
      codeUrl: process.env.VUE_APP_URL + "/captcha?type=login",
      form: {
        phone: "",
        password: "",
        code: "",
        ispass: [],
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          { min: 11, max: 11, message: "请输入11位手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              let feach = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (feach.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号"));
              }
            },
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 6,
            max: 12,
            message: "请输入6到12位的密码",
            trigger: "change",
          },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          {
            min: 4,
            max: 4,
            message: "请输入4位验证码",
            trigger: "change",
          },
        ],
        ispass: [{ required: true, message: "请勾选协议", trigger: "change" }],
      },
    };
  },
  methods: {
    gocode() {
      this.codeUrl =
        process.env.VUE_APP_URL + "/captcha?type=login&fdfyhfgh=" + Date.now();
    },
    tologin() {
      this.$refs.form.validate((result) => {
        // result为true
        if (result) {
          // this.$message.success("登录成功");
          userLogin(this.form).then((res) => {
            console.log(res);
            // window.localStorage.setItem("token", res.data.data.token);
            saveLocal("token", res.data.data.token);
            this.$router.push("/layout");
          });
        } else {
          this.$message.error("登录失败");
        }
      });
    },
    cancleEven() {
      this.$refs.register.isShow = true;
    },
  },
  created() {
    if (getLocal("token")) {
      this.$router.push("/layout");
    }
  },
};
</script>

<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 43px;
    .main {
      display: flex;
      align-items: center;
      .t1 {
        width: 25px;
        height: 18px;
      }
      .t2 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin-left: 13px;
        margin-right: 14px;
      }
      .t3 {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin-right: 12px;
      }
      .t4 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 30px;
      .login_img {
        height: 40px;
        width: 100%;
      }
      .btn {
        display: block;
        width: 100%;
      }
      .tiao {
        vertical-align: bottom;
      }
    }
  }
}
</style>
